<template>
  <div class="c-eqpt-type-add">
    <c-row-crumb :title="multLang('title_asset_c', 'btn_new_a')">
      <el-button @click="clear">{{ multLang('btn_clear_a') }}</el-button>
      <el-button type="primary" plain @click="submit(true)">
        {{ multLang('保存并返回') }}
      </el-button>
      <el-button type="primary" @click="submit()">
        {{ multLang('btn_save_a') }}
      </el-button>
    </c-row-crumb>

    <c-row-add :title="tplLang('title_asset_e', ['title_asset_c'])">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="140px"
        :validate-on-rule-change="false"
      >
        <c-row :col="4">
          <c-col>
            <el-form-item
              :label="multLang('title_name_b', ':')"
              prop="category_name"
            >
              <el-input
                v-model="form.category_name"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item
              :label="multLang('title_category_a', ':')"
              prop="type"
            >
              <c-select
                v-model="form.type"
                :options="assetCategoryTypeList"
                :placeholder="tplLang('text_choose_a', ['title_category_a'])"
              ></c-select>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item
              :label="multLang('title_e', ':')"
              prop="category_code"
            >
              <el-input
                v-model="form.category_code"
                :placeholder="multLang('text_d')"
              ></el-input>
            </el-form-item>
          </c-col>
          <c-col>
            <el-form-item :label="multLang('title_f', ':')" prop="parent_id">
              <c-cascader
                v-model="form.parent_id"
                :options="assetTypeAllTree"
                clearable
                :placeholder="tplLang('text_choose_a', ['title_f'])"
              ></c-cascader>
            </el-form-item>
          </c-col>
          <c-col :col="11">
            <el-form-item :label="multLang('title_note_a', ':')" prop="remark">
              <c-input-text v-model="form.remark"></c-input-text>
            </el-form-item>
          </c-col>
        </c-row>
      </el-form>
    </c-row-add>
  </div>
</template>
<script>
import { addAssetType } from '@/http/api/v1/eqpt-type'
export default {
  name: 'CEqptTypeAdd',
  components: {},
  props: {},
  data() {
    return {
      form: {
        category_name: '',
        type: '',
        category_code: '',
        parent_id: '',
        remark: ''
      }
    }
  },
  computed: {
    assetTypeAllTree() {
      return this.optionTree('asset_type_all')
    },
    assetCategoryTypeList() {
      return this.optionList('asset_category_type')
    },
    rules() {
      return {
        category_name: {
          required: true,
          message: this.tplLang('title_name_w', ['title_name_b']),
          trigger: 'blur'
        },
        type: {
          required: true,
          message: this.tplLang('title_name_w', ['title_category_a']),
          trigger: 'change'
        }
      }
    }
  },
  watch: {},
  beforeCreate() {},
  created() {
    this.setOption({
      key: 'asset_category_type'
    })
  },
  beforeMount() {},
  mounted() {},
  beforeUpdate() {},
  updated() {},
  activated() {
    this.getAssetType()
  },
  deactivated() {},
  beforeDestroy() {},
  destroyed() {},
  methods: {
    getAssetType() {
      this.setOption({
        key: 'asset_type_all'
      })
    },
    submit(back) {
      addAssetType({
        ...this.form
      }).then(({ code }) => {
        if (code === 200) {
          this.getAssetType()
          back && this.$router.back()
        }
      })
    },
    clear() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss">
.c-eqpt-type-add {
}
</style>
